package util;
import meal.*;
import java.util.*;
import java.io.*;

//Margarita Lee:mleeli | Last Updated: 10.29.11 -Created the IO class with writeMeals and write methods.

public class IO{
	//Margarita Lee:mleeli | Last Updated:10.29.11
	//Task: Start the writeMeals contructor method.
	/**
	* Defines String name as the meal's name and the String str as the formatted
	* meal's information.
	*/
	public static void writeMeals(Meal meal[]) {
		for(int i = 0; i <meal.length; i++){
			String name = meal[i].getName() +".txt";
			String str = MealFormatting.formatMeal(meal[i]);
		}
	}
	//End of the constructor.

	//Margarita Lee:mleeli | Last Updated:10.29.11
	//Task: Start the write constructor method.
	/**
	* Creates a new file with that is called name (the meal's name) and writes the meal's information
	* in the file.
	*/
	public static void write(String name, String str) {
		PrintWriter dataOutFile = null;
		try{
			dataOutFile = new PrintWriter(name);
		}catch (Exception e){
			System.exit(0);
		}
		dataOutFile.println(str);
		dataOutFile.close();
	}
	//End of the constructor.


	public static Meal importMeal(File path) {
			try {
			Scanner scanner = new Scanner(path);
			String name = scanner.nextLine();
			double rating = Double.parseDouble(scanner.nextLine());
			int cnt = Integer.parseInt(scanner.nextLine());

			Ingredient ingr[] = new Ingredient[cnt];
			for(int i=0; i<cnt; i++)
				ingr[i] = new Ingredient(scanner.nextLine());

			cnt = Integer.parseInt(scanner.nextLine());
			Direction dir[] = new Direction[cnt];
			for(int i=0; i<cnt; i++)
				dir[i] = new Direction(scanner.nextLine());
			return new Meal(name, ingr, dir, rating);
			} catch(Exception e) {}
			return null;
	}
	public static Meal importMeal(String path) {
		try {
			return importMeal(new File(path));
		} catch (Exception e) {
		}
		return null;

	}
	public static Meal[] importMeals(String path) {
		try {
		File file = new File(path);
		File[] files = file.listFiles();
		Meal[] meals = new Meal[files.length];
		for(int i=0; i<meals.length; i++)
			meals[i]=importMeal(files[i]);
		return meals;
		} catch(Exception e) {}
		return null;
	}
	public static void main(String[] args) {
		System.out.println(Arrays.toString(importMeals("recipes/")));
	}
}
